﻿using System;
using System.Diagnostics;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YJKCodeBase.WinForm;
using YRevitJig;
using YJKRGeometry;
using YJKView;

namespace YJKCoordinate
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdDitchDrawWSD : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Application application2 = application.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Utiltiy.m_doc = document;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			string appRootPath = Utiltiy.GetAppRootPath();
			string versionNumber = application.Application.VersionNumber;
			string strUseFamilyPath = appRootPath + "\\FamilyLib\\Family\\" + versionNumber + "\\Ditch\\";
			StaticRescource.SetStrUseFamilyPath(strUseFamilyPath);
			StaticRescource.ParamXmlPath = appRootPath + "\\Data\\";
			string strPathFamilyTemplate = appRootPath + "\\Templates\\" + versionNumber + "\\公制常规模型.rft";
			StaticRescource.SetStrPathFamilyTemplate(strPathFamilyTemplate);
			StaticRescource.SetStrPathOutPut(Path.GetTempPath());
			Process currentProcess = Process.GetCurrentProcess();
			IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
			ViewAssist.ViewValidating(activeUIDocument.Document, activeUIDocument.ActiveView);
			YRevitJigExtend hyjigExtend = new YRevitJigExtend(activeUIDocument);
			FormUse formUse = new FormUse(mainWindowHandle, hyjigExtend, this.formIndex);
			DrainMakerApplication drainMakerApplication = new DrainMakerApplication(application, document, this.formIndex);
			formUse.Show(new WindowHandle(mainWindowHandle));
			Result result;
			try
			{
				formUse.Refresh();
				this.getPointLoop(activeUIDocument, drainMakerApplication, hyjigExtend, formUse);
				if (formUse != null && !formUse.IsDisposed)
				{
					formUse.Close();
				}
				if (formUse.GetIfDrawSlope())
				{
					drainMakerApplication.DrawSlopeTag();
				}
				ProgressBarForm progressBarForm = null;
				drainMakerApplication.SetFamilies(ref progressBarForm, mainWindowHandle);
				drainMakerApplication.DeleteAllDetailCurves();
				if (progressBarForm != null)
				{
					progressBarForm.Close();
				}
				hyjigExtend.Dispose();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				try
				{
					hyjigExtend.Dispose();
					formUse.Close();
				}
				catch (Exception)
				{
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		internal void SetFormIndex(int input = 0)
		{
			this.formIndex = input;
		}

		private XYZ getPointLoop(UIDocument ui_doc, DrainMakerApplication thisApplicantion, YRevitJigExtend tempYRevitJigExtend, FormUse tempForm)
		{
			XYZ xyz;
			do
			{
				xyz = null;
				tempForm.IfSendEscTag = true;
				if (thisApplicantion.GetEndPointNow() == null)
				{
					xyz = tempYRevitJigExtend.PickSrartPointEX("请选择一点，ESC结束");
					tempForm.LockControles();
				}
				else
				{
					xyz = tempYRevitJigExtend.PickEndPointEX(thisApplicantion.GetEndPointNow(), "请选择一点，ESC结束", false);
				}
				if (xyz != null)
				{
					if (thisApplicantion.GetEndPointNow() != null && Geometry.LessThan(thisApplicantion.GetEndPointNow().DistanceTo(xyz), tempForm.NowLengthLimitValue))
					{
						YJKMessageBox.Show("输入过短请重新选择");
					}
					else
					{
						OneInputDataPacker input;
						try
						{
							input = new OneInputDataPacker(xyz, tempForm.GetCornerKind(), tempForm.GetParameters(), tempForm.GetFamilyFileIndex());
						}
						catch (CornerMakerException ex)
						{
							if (ex.Message.Equals(StaticRescource.StrError_WrongParameter))
							{
								YJKMessageBox.Show(StaticRescource.StrMessage_WrongParameter);
								goto IL_19F;
							}
							throw ex;
						}
						thisApplicantion.AddOnePoint(input);
					}
				}
				else
				{
					tempForm.IfSendEscTag = false;
					if (DrainMakerApplication.UseMessageTag)
					{
						thisApplicantion.AddOnePoint(null);
					}
					else
					{
						tempForm.Visible = false;
						double input2 = -0.1;
						XYZ[] array = new XYZ[2];
						XYZ[] array2 = array;
						for (int i = 0; i <= 1; i++)
						{
							try
							{
								if (i == 0)
								{
									array2[i] = ui_doc.Selection.PickPoint("请选一点");
								}
								else
								{
									array2[i] = tempYRevitJigExtend.PickEndPointEX(array2[0], "请选另一点", true);
								}
							}
							catch (Exception)
							{
								array2[i] = null;
								break;
							}
						}
						if (array2[0] != null && array2[1] != null && !array2[0].IsAlmostEqualTo(array2[1]))
						{
							input2 = array2[0].DistanceTo(array2[1]);
						}
						else
						{
							YJKMessageBox.Show("量取有误");
						}
						tempForm.SetComboBoxValue(Utiltiy.FeetMMChange(input2, true));
						tempForm.Visible = true;
						DrainMakerApplication.UseMessageTag = true;
						xyz = XYZ.Zero;
						tempForm.FreeControlTag = false;
					}
				}
				IL_19F:;
			}
			while (xyz != null);
			return xyz;
		}

		private int formIndex;
	}
}
